home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / exampleCode / irix / tools / rcstime.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  6.0 KB  |  232 lines

  1. /*------------------------------------------------------------------------
  2.  * $Id: rcstime.c,v 1.5 1993/12/02 09:54:59 carlson Exp $
  3.  *
  4.  * rcstime    Returns the time of the last RCS update in an RCS file.
  5.  *
  6.  * Synopsis:
  7.  *    rcstime [+<format>] [-i] [-R] [-r<rev>] file [[-r<rev>] files...]
  8.  *
  9.  * Description:
  10.  *    This program writes to stdout the time of the last update found
  11.  *    in the RCS file.  The program uses the same search sequence for
  12.  *    the RCS file as the standard RCS programs (co, ci, rcs).  For
  13.  *    information on how it finds the RCS file, see below under File
  14.  *    Search Method.
  15.  *
  16.  *    The -r<rev> option identifies which revision from the RCS file
  17.  *    is to be used in computing the modification time.  If just -r
  18.  *    is given (with no <rev>), the latest known revision is selected.
  19.  *
  20.  *    The +<format> option provides a format that is to be used in
  21.  *    writing the time.  This format is identical to the date(1)
  22.  *    command.
  23.  *
  24.  *    The -i option overrides any format provided and prints the date
  25.  *    in integer format.
  26.  *
  27.  *    The -R option specifies that the actual revision number that was
  28.  *    found should also be printed.
  29.  *
  30.  * File Search Method:
  31.  *    If the file name ends in ",v", it is take to be an RCS file.
  32.  *    All other files are assumed to be working files and the names
  33.  *    are converted be RCS files.  If no path is provided, the RCS
  34.  *    file is looked for in the directory ./RCS and then in the
  35.  *    current directory.  If a path is provided, the RCS file is
  36.  *    looked for only in the directory provided.
  37.  *
  38.  * Revision History:
  39.  *    $Log: rcstime.c,v $
  40.  * Revision 1.5  1993/12/02  09:54:59  carlson
  41.  * Found an RCS file that didn't have a comment line.  Needed to take out
  42.  *   the part where I search for the comment line and modify the loop which
  43.  *   looks for revisions to ignore lines until it gets to a revision number.
  44.  *
  45.  * Revision 1.4  93/11/30  15:22:31  carlson
  46.  * Modified to now give the latest time found in the RCS file.
  47.  * Add -R option to provide the revision found.
  48.  * 
  49.  * Revision 1.3  92/10/27  10:33:10  carlson
  50.  * Found that I was printing the address and not the time when printing
  51.  *   the time in decimal.
  52.  * 
  53.  * Revision 1.2  91/07/19  15:15:39  carlson
  54.  * Added Header for RCS.
  55.  * Added -i option for printing time in decimal.
  56.  * Modified to handle new format of cftime using %.
  57.  * 
  58.  * Revision 1.1  90/10/09  17:26:51  chris
  59.  * Initial revision
  60.  * 
  61.  * 07 Feb 90  Christopher W. Carlson, Silicon Graphics Inc.
  62.  * Initial draft.
  63.  *
  64.  *-----------------------------------------------------------------------*/
  65.  
  66. #include <stdio.h>
  67. #include <errno.h>
  68. #include <ctype.h>
  69. #include <sys/types.h>
  70. #include <time.h>
  71. #include <string.h>
  72.  
  73. #define FALSE        0
  74. #define TRUE        1
  75.  
  76. static char    cbuff[256], format[80] = "", rev[20] = "";
  77. static char    filename[128], foundRev[20];
  78. static int    year, month, day, hour, minute, second;
  79. static time_t    ltime, ntime;
  80. static int    months[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  81. static int    intfmt, R_opt = FALSE;
  82.  
  83. main (argc, argv)
  84.   int argc;
  85.   char *argv[];
  86. {
  87.     register int i, j, k;
  88.     register char *c;
  89.     register struct tm *tmptr;
  90.     FILE *fp;
  91.     char    thisRev[20];
  92.  
  93.     intfmt = FALSE;
  94.  
  95.     for (i = 1; i < argc; i++) {
  96.  
  97.     if (*argv[i] == '+') {
  98.  
  99.         for (j = 0, c = &argv[i][1]; *c; c++) {
  100.         format[j++] = '%';
  101.         format[j++] = *c;
  102.         }
  103.  
  104.     } else if (*argv[i] == '-') {
  105.  
  106.         switch (argv[i][1])
  107.         {
  108.           case 'i':
  109.         intfmt = (intfmt) ? FALSE : TRUE;
  110.         break;
  111.  
  112.           case 'r':
  113.         strcpy (rev, &argv[i][2]);
  114.         break;
  115.  
  116.           case 'R':
  117.         R_opt = TRUE;
  118.         break;
  119.  
  120.           default:
  121.         fprintf (stderr, "rcstime: Unrecognized option %s\n", argv[i]);
  122.         continue;
  123.         }
  124.  
  125.     } else {        /* Must be a filename */
  126.  
  127.         if (index (argv[i], '/') == NULL) {
  128.         strcpy (filename, "./RCS/");
  129.         strcat (filename, argv[i]);
  130.         j = strlen (filename) - 2;
  131.         if ((filename[j] != ',') || (filename[j+1] != 'v'))
  132.             strcat (filename, ",v");
  133.         if (access (filename, 4))
  134.             strcpy (filename, argv[i]);
  135.         } else {
  136.         strcpy (filename, argv[i]);
  137.         }
  138.         j = strlen (filename) - 2;
  139.         if ((filename[j] != ',') || (filename[j+1] != 'v'))
  140.         strcat (filename, ",v");
  141.         if (access (filename, 4)) {
  142.         fprintf (stderr, "rcstime: Can't find RCS file %s\n", argv[i]);
  143.         continue;
  144.         }
  145.  
  146.         if ((fp = fopen (filename, "r")) == NULL) {
  147.         fprintf (stderr, "rcstime: Something wrong with opening the");
  148.         fprintf (stderr, "         RCS file.  Major problem = %d\n",
  149.              errno);
  150.         exit (1);
  151.         }
  152.  
  153.             /*----
  154.          * Search for desired revision number or, if none given,
  155.          * search all revisions.
  156.          *----*/
  157.  
  158.         k = strlen (rev);
  159.         ntime = 0;
  160.         foundRev[0] = '\0';
  161.         while (fgets (cbuff, 256, fp)) {
  162.         if (strncmp (cbuff, "desc", 4) == 0) {
  163.             if (ntime == 0)
  164.             {
  165.             fprintf (stderr,
  166.                  "rcstime: Can't find requested revision %s in file %s\n",
  167.                  rev, argv[i]);
  168.             break;
  169.             }
  170.  
  171.             if (intfmt) {
  172.             if (R_opt)
  173.                 printf ("%s  ", foundRev);
  174.             printf ("%d\n", ntime);
  175.             } else if (format[0] == '\0') {
  176.             if (R_opt)
  177.                 printf ("%s  ", foundRev);
  178.             c = ctime (&ntime);
  179.             printf ("%s", c);
  180.             } else {
  181.             if (R_opt)
  182.                 printf ("%s  ", foundRev);
  183.             cftime (cbuff, format, &ntime);
  184.             printf ("%s\n", cbuff);
  185.             }
  186.  
  187.             break;
  188.         }
  189.  
  190.         j = strlen (cbuff) - 1;
  191.         cbuff[j] = '\0';
  192.         if ( ! isdigit (cbuff[0]))
  193.             continue;
  194.         if ((k == 0) ||
  195.             ((k != 0) && (strncmp (cbuff, rev, k) == 0))) {
  196.  
  197.             strcpy (thisRev, cbuff);
  198.             fgets (cbuff, 256, fp);
  199.             if (strncmp (cbuff, "date", 4) != 0) {
  200.             fprintf (stderr,
  201.                  "rcstime: Improperly formatted RCS file %s\n",
  202.                  argv[i]);
  203.             break;
  204.             }
  205.  
  206.             tzset ();
  207.             sscanf (cbuff, "%*s %2d.%2d.%2d.%2d.%2d.%2d;", &year,
  208.                 &month, &day, &hour, &minute, &second);
  209.             ltime = year - 70;
  210.             ltime = (ltime * 365) +
  211.                 ((ltime + ((month > 2) ? 2 : 1)) / 4);
  212.             month--;
  213.             for (j = 0; j < month; j++)
  214.             ltime += months[j];
  215.             ltime += day - 1;
  216.             ltime = (ltime * 24) + hour;
  217.             ltime = (ltime * 60) + minute;
  218.             ltime = (ltime * 60) + second + timezone;
  219.  
  220.             if (ltime > ntime)
  221.             {
  222.             ntime = ltime;
  223.             strcpy (foundRev, thisRev);
  224.             }
  225.         }
  226.         }
  227.  
  228.         fclose (fp);
  229.     }
  230.     }
  231. }
  232.